<?php
/**
 * VDONG Project
 * ============================================================================
 * Copyright (c) 2016 www.vdchina.com
 * ----------------------------------------------------------------------------
 * 文件名称：index.php
 * ----------------------------------------------------------------------------
 * 功能描述：商品规格
 * ----------------------------------------------------------------------------
 * 作者：liyinglong
 * ----------------------------------------------------------------------------
 */
namespace app\specification\admin;


use app\common\builder\ZBuilder;
use app\specification\model\SpecificationType as SpecificationTypeModel;
use app\specification\model\Specification as SpecificationModel;
use app\admin\controller\Admin;
use app\common;
/**
 * 商品规格模块控制器
 * @package app\specification\controller
 */
class Specification extends Admin
{   
       /**
        *   构造函数
        *@param    int     notdel         数据状态   未删除
        *@param    int     del            数据状态   已经删除
        *@param    int     pid            顶级ID     默认为0
        */ 
    public function __construct(){
        parent::__construct();
            define('notdel',2);  //未删除
            define('del',1);     //删除
            define('pid',0);     //
    }

       /**
        *商品规格列表
        *@param    int    notdel          数据状态   未删除
        */ 
    public function index(){

    //*********************************************************************************

        cookie('__forward__', $_SERVER['REQUEST_URI']);

        $where['isdel'] =  notdel;
        // 数据列表
        $data_list = SpecificationTypeModel::where($where)->paginate();

        // 分页数据
        $page = $data_list->render();

        //下一级按钮
        $btn_access = [
          'title' => '下级',
          'icon'  => 'fa fa-fw fa-plus-square',
          'href'  => url('specificationList', ['id' => '__id__'])
        ];

        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('规格分类') // 设置页面标题
            ->addColumns([ // 批量添加列
                ['id', 'ID'],
                ['name', '用户名'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButtons('add,delete') // 批量添加顶部按钮
            ->addRightButton('custom', $btn_access) // 添加下一级按钮
            ->addRightButtons('edit,delete') // 批量添加右侧按钮
            ->setRowList($data_list) // 设置表格数据
            ->setPages($page) // 设置分页数据
            ->fetch(); // 渲染页面
            

    //********************************************************************************
    }

      
     /**
      *  规格添加
      *@param    int    notdel          数据状态   未删除
      */ 
      public  function  add(){

            if($this->request->isPost()){

                   $data['name'] = $_POST['name'];
                   $data['isdel'] = notdel;
                    if (SpecificationTypeModel::create($data)) {
                        $this->success('添加成功', cookie('__forward__'));
                    } else {
                        $this->error('添加失败');
                    }

            }else{

               return ZBuilder::make('form')
                     ->addText('name', '标题')
                    ->fetch();

            }
 
       }


     /**
      *  规格删除
      *@param    int    del             数据状态   已删除
      *@param    int    ids             选择的数据ID，可以是多条数据
      */
     
     public   function  delete($ids = ''){
           
         
          $ids = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
           
          if (empty($ids)) $this->error('缺少主键');
            

             $data['isdel'] = del;
           
             if(SpecificationTypeModel::where('id', 'in', $ids)->update($data)) {
              
               
                      $count = SpecificationModel::where('typeid', 'in', $ids)->count();
                      if($count>0){

                                 if(SpecificationModel::where('typeid', 'in', $ids)->update($data)){
                                       
                                          $this->success('删除成功');
                                 }else{

                                          $this->error('删除失败');
                                    
                                 }
                            
                      }else{

                           $this->success('删除成功');
                      }
                     
                 
           } else {
              $this->error('删除失败');
           }
            
     }
    
     /**
      * 规格编辑
      * @param    int    id             选择的要修改的数据ID
      */
    public function edit($id=""){

        if ($id === null) $this->error('缺少参数');
       
        // 保存数据
        if ($this->request->isPost()) {

            $data = $this->request->post();
            $result = SpecificationTypeModel::where('id', $id)->update($data);
            if ($result!==false) {
                $this->success('编辑成功', cookie('__forward__'));
            } else {
                $this->error('编辑失败');
            }
        }else{
              // 获取数据
              $info = SpecificationTypeModel::where('id', $id)->find();

                   return ZBuilder::make('form')
                           ->addText('name', '标题')
                           ->setFormData($info) // 设置表单数据
                           ->fetch();    
        }
    }


     /**
      * 规格属性的列表
      * @param    int    id         点击规格列表下级按钮传过来的ID
      * @param    int    notdel      数据状态  未删除
      */

    public  function specificationList($id){

           cookie('__forward__', $_SERVER['REQUEST_URI']);

            $where['isdel'] =  notdel;
            $where['typeid'] =  $id;
            // 数据列表
            $data_list = SpecificationModel::where($where)->paginate();
            
            //分页数据
            $page = $data_list->render();
            //编辑按钮
            $btn_access = [
              'title' => '编辑',
              'icon'  => 'fa fa-fw fa-pencil',
              'href'  => url('specificationEdit', ['id' => '__id__'])
             ];

            //删除按钮
            $btn_access2 = [
              'title' => '删除',
              'icon'  => 'fa fa-fw fa-remove',
              'href'  => url('specificationDel', ['id' => '__id__'])
             ];
        
            //添加按钮
              $btn_access3 = [
                  'title' => '添加',
                  'icon'  => 'fa fa-fw fa-key',
                  'href'  => url('specificationAdd', ['id' => $id])
              ];
          
        
            // 使用ZBuilder快速创建数据表格
            return ZBuilder::make('table')
                ->setPageTitle('规格内容') // 设置页面标题
                ->addColumns([ // 批量添加列
                    ['id', 'ID'],
                    ['name', '名称'],
                    ['plusprice', '加价'],
                    ['right_button', '操作', 'btn']
                ]) 
                ->addTopButton('custom', $btn_access3) // 增加添加按钮
                ->addRightButton('custom', $btn_access) // 添加编辑按钮
                ->addRightButton('custom', $btn_access2) // 添加删除按钮
                ->setRowList($data_list) // 设置表格数据
                ->setPages($page) // 设置分页数据
                ->fetch(); // 渲染页面

    }

    
     
     /**
      * 规格属性添加
      * @param    int    id          规格属性上级ID
      * @param    int    notdel      数据状态  未删除
      */
      public  function  specificationAdd($id){
            
          
            if ($id === null) $this->error('缺少参数');

            if($this->request->isPost()){
 
                     $data['name'] = $_POST['name'];
                     $data['typeid'] = $id;
                     $data['plusprice'] = $_POST['plusprice'];
                     $data['isdel'] = notdel;
                      if (SpecificationModel::create($data)) {
                          $this->success('添加成功', cookie('__forward__'));
                      } else {
                          $this->error('添加失败');
                      }

              }else{
                 
                 return ZBuilder::make('form')
                        ->addText('name',      '名称')
                        ->addText('plusprice', '加价')
                      ->fetch();

              }
    }

     /**
      * 规格属性编辑
      * @param    int    id          要编辑数据的ID
      * @param    
      */
        public  function specificationEdit($id){

               if ($id === null) $this->error('缺少参数');
   
                  // 保存数据
                  if ($this->request->isPost()) {

                        $data['name'] = $_POST['name'];
                        $data['plusprice'] = $_POST['plusprice'];
                        $result = SpecificationModel::where('id', $id)->update($data);
                      if ($result!==false) {
                          $this->success('编辑成功', cookie('__forward__'));
                      } else {
                          $this->error('编辑失败');
                      }
                  }else{
                        // 获取数据
                        $info = SpecificationModel::where('id', $id)->find();

                             return ZBuilder::make('form')
                                    ->addText('name',      '名称')
                                    ->addText('plusprice', '加价')
                                    ->setFormData($info) // 设置表单数据
                                    ->fetch();    
                  }
        }

       /**
        * 规格属性删除
        * @param    int    id     要删除数据的ID
        * @param    int    del  数据状态   已删除
        */
        public function specificationDel($id){
          
             if (empty($id)) $this->error('缺少主键');
               $where['id'] = $id;
               $data['isdel'] = del;

             if (SpecificationModel::where($where)->update($data)) {
                 
                     $this->success('删除成功');

              } else {
                $this->error('删除失败');
             }
        }

       /**
        * 要返回的规格接口数据
        * @param    int    projectid     项目ID
        * @param    int    notdel        数据状态   未删除
        */
      public function returnSpecification($projectid=""){
           
              if(empty($projectid)){
                     outFormat(1,'商品ID不存在');
               }
               $where['projectid'] = $projectid;
               $where['isdel'] = notdel ;     
               $order   = 'id desc';        //排序方式
            
               $result = SpecificationTypeModel::where($where)->order($order)->select();
             
               foreach ($result as $key => $value) {
                  
                    $result1 = SpecificationModel::where('typeid',$value['id'])->order($order)->select();
                    
                    $result1 = json_decode(json_encode($result1),true);
                    
                    $result[$key]['specifi'] =$result1;
                   
               }
               
               return (json_decode(json_encode($result),true));
        
            
      }


}